package com.yuantu.judiciary.dao;

import com.yuantu.judiciary.model.domain.DiscountCodeDO;
import com.yuantu.judiciary.model.form.DiscountCodeForm;
import com.yuantu.judiciary.model.form.DiscountCodeStatusForm;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * 动态信息
 *
 * @author syw
 * @since 2023-08-21
 **/
public interface IDiscountCodeDao extends JpaRepository<DiscountCodeDO, Long> {


    DiscountCodeDO findDiscountCodeByDiscountCodeAndDeletedFalse(String discountCode);

    DiscountCodeDO findDiscountCodeByIdAndDeletedFalse(Long id);


    DiscountCodeDO findDiscountCodeByAccountInfoAndIsValidAndDeletedFalse(String accountInfo, Boolean isValid);

    @Modifying
    @Query("update DiscountCodeDO set deleted = true where deleted = false and id in (:discountCodeIds) ")
    void removeDiscountCodeInfo(List<Long> discountCodeIds);

    @Query("select dc from DiscountCodeDO dc where dc.deleted = false " +
            "and (:#{#form.owner} is null or :#{#form.owner} = '' or dc.owner like concat('%', :#{#form.owner}, '%')) " +
            "and (:#{#form.discountCode} is null or :#{#form.discountCode} = '' or dc.discountCode like concat('%', :#{#form.discountCode}, '%')) ")
    Page<DiscountCodeDO> findDiscountCodeInfosByForm(@Param("form") DiscountCodeForm form, Pageable pageable);

    @Modifying
    @Query("update DiscountCodeDO set isValid = :#{#form.isValid} where id = :#{#form.discountCodeId} and deleted = false ")
    void changeDiscountCodeStatus(@Param("form") DiscountCodeStatusForm form);

    List<DiscountCodeDO> findInfosByAccountInfoInAndIsValidAndDeletedFalse(List<String> phones, Boolean isValid);

    @Query("select d from DiscountCodeDO d where d.deleted = false ")
    List<DiscountCodeDO> findAllInfos();


}
